package org.visionlibrary.image.filters.bluring;

import java.awt.Point;

import org.visionlibrary.image.model.ConvolutionKernel;


public class Pyramidal {
	protected int size = 5;
	protected Point origin = new Point(2,2);

	public Pyramidal() {
		this(3);
	}
	
	public Pyramidal(int size) {
		super();
		this.size = size;
	}
	
	public ConvolutionKernel getFilterKernel() {
		if(size == 0)
			return null;
		float[] values = new float[size*size];
		
		int i = 0;
		int k = 1;
		int d1 = values.length / 2;
		d1 = d1 / size;
		d1 = d1 * size;
		int d2 = size / 2;
		boolean odd1 = size * size - 2*d1 != 0;
		boolean odd2 = size - 2*d2 != 0;
		
		while(i < d1) {
			int j = 0;
			int sum = 0;
			
			while(j < d2) {
				sum += k;
				values[j + i] = sum;
				j++;
			}
			if(odd2) {
				sum += k;
				values[j + i] = sum;
				j++;
			}
			while(j < size) {
				sum -= k;
				values[j + i] = sum;
				j++;
			}
			i+=size;
			k++;
		}
		if(odd1) {
			int j = 0;
			int sum = 0;
			
			while(j < d2) {
				sum += k;
				values[j + i] = sum;
				j++;
			}
			if(odd2) {
				sum += k;
				values[j + i] = sum;
				j++;
			}
			while(j < size) {
				sum -= k;
				values[j + i] = sum;
				j++;
			}
			i+=size;
			k--;
		}
		while(i < values.length) {
			int j = 0;
			int sum = 0;
			
			while(j < d2) {
				sum += k;
				values[j + i] = sum;
				j++;
			}
			if(odd2) {
				sum += k;
				values[j + i] = sum;
				j++;
			}
			while(j < size) {
				sum -= k;
				values[j + i] = sum;
				j++;
			}
			i+=size;
			k--;
		}
		
		float summ = 0;
		for(float ii : values)
			summ += ii;
	
		return new ConvolutionKernel(size, size, values, summ, new Point(size/2, size/2));
	}
	
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}

	public Point getOrigin() {
		return origin;
	}

	public void setOrigin(Point origin) {
		this.origin = origin;
	}
}
